package com.wangbo.basic;

public class LinkedListStack<T> {
    // 栈顶
    public Node<T> top;

    // 节点类
    public static class Node<T>{
        public T data;
        public Node<T> next;

        public Node(T data) {
            this.data = data;
        }
    }

    // 是否为空
    public boolean isEmpty() {
        return top == null;
    }

    // 新增节点
    public void push(T data) {
        Node<T> node = new Node(data);
        node.next = top;
        top = node;
    }

    // 弹出栈顶
    public Node<T> pop() {
        if (isEmpty()) {
            throw new CustomException(400, "栈为空");
        }
        Node<T> temp = top;
        top=top.next;
        return temp;
    }

    // 获取栈顶，不弹出
    public Node<T> peek() {
        return top;
    }

    // 获取栈容量
    public int size() {
        int count = 0;
        Node<T> temp = top;
        while (temp != null) {
            count++;
            temp = temp.next;
        }
        return count;
    }
}
